<template>
  <div>
    <!-- 表格 -->
    <el-table :data="records" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称" width="200"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="240"></el-table-column>
      <el-table-column label="默认图片" width="100">
        <template slot-scope="{row}">
          <el-image style="width: 80px; height: 80px" :src="row.skuDefaultImg"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80"></el-table-column>
      <el-table-column prop="price" label="价格" width="80"></el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{row}">
          <el-button
            type="success"
            icon="el-icon-bottom"
            size="mini"
            v-if="row.isSale == 0"
            @click="sale(row)"
          ></el-button>
          <el-button type="success" icon="el-icon-top" size="mini" v-else @click="cancel(row)"></el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteSku(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="currentPageChange"
      @size-change="handleSizeChange"
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->, sizes, total"
      :total="total"
    ></el-pagination>
    <!-- 抽屉效果 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfo.skuName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfo.price}}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            style="margin-right:10px"
            v-for="attr in skuInfo.skuAttrValueList"
            :key="attr.id"
          >{{attr.attrId}}-{{attr.valueId}}</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width:200px; height:200px" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data () {
    return {
      page: 1,
      limit: 10,
      records: [], // 存储从服务器获取到的skuList,用来在页面展示
      total: 0, //存储一共有多少页的字段
      skuInfo: {}, //存储从服务器获取到的skuInfo数据
      drawer: false, //控制抽屉的显示与隐藏


    }
  },

  mounted () {
    // 获取sku列表的方法
    this.getSkuList()
  },

  methods: {
    // 获取服务器返回的skuList
    async getSkuList () {
      const { page, limit } = this;
      let result = await this.$API.sku.reqSkuList(page, limit);
      // console.log(result)
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    // 点击分页器页码数时的回调
    currentPageChange (pager) {
      this.page = pager;
      // 请求数据
      this.getSkuList()
    },
    // 改变每页展示数据的条数事件的回调
    handleSizeChange (limit) {
      this.limit = limit;
      // 再次请求数据
      this.getSkuList()
    },
    // 点击上架按钮的回调
    async sale (row) {
      let result = await this.$API.sku.reqSale(row.id)
      // console.log(result);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message({ type: 'success', message: '上架成功' })
      }
    },
    // 点击下架按钮的回调
    async cancel (row) {
      let result = await this.$API.sku.reqCancel(row.id)
      // console.log(result);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message({ type: 'success', message: '下架成功' })
      }
    },
    // 点击编辑按钮的回调
    edit () {
      this.$message('正在开发中')
    },
    // 点击查看sku按钮事件的回调
    async getSkuInfo (sku) {
      // 显示抽屉
      this.drawer = true;
      let result = await this.$API.sku.reqSkuById(sku.id)
      console.log(result);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
    // 点击删除sku的回调
    async deleteSku(sku){
      let result = await this.$API.sku.reqDeleteSku(sku.id)
      // console.log(result);
      if(result.code == 200){
        this.$message({type: 'success', message: '删除成功'})
        this.getSkuList()
      }
    }
  }
}
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
</style>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

</style>